package com.movie.act;

import android.content.Context;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.View;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.MapView;
import com.google.android.maps.OverlayItem;
import com.movie.map.MapPoi;
import com.movie.map.MyItemizedOverlay;
import com.movie.util.Log;

/**
 * 19-影院地图
 * 
 */
public class OneCinaemaAct extends BaseMapActivity {
	GeoPoint myGeoPoint = null;
	private LocationManager gpsLocationManager = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		initBackTitleBar();
		setContentView(R.layout.onecinaemaact);
		// 获取点
		myGeoPoint = new GeoPoint(39909230, 116397428);
		// 初始化图
		initMap();
		// 显示图层
		mapView.getController().animateTo(myGeoPoint);
		showPosition();
		// 点击事件
		myItemizedOverlay.setOnFocusChangeListener(new MyOverlay());
	}

	OverlayItem overlayitem = null;
	MyItemizedOverlay myItemizedOverlay;

	private void showPosition() {
		OverlayItem overlayitem = new OverlayItem(myGeoPoint, null, null);
		myItemizedOverlay = new MyItemizedOverlay(getResources().getDrawable(
				R.drawable.play_icon), this);
		myItemizedOverlay.addOverlay(overlayitem);
		MapPoi goods = new MapPoi();
		goods.setmGeoPoint(myGeoPoint);
		goods.setType(1);
		myItemizedOverlay.addCurrentOverlayData(goods);
		mapView.getOverlays().add(myItemizedOverlay);

	}

	private MapView mapView;

	private void initMap() {
		Log.i("-------onCreate---initMap------");
		mapView = (MapView) findViewById(R.id.mapview);
		gpsLocationManager = (LocationManager) this
				.getSystemService(Context.LOCATION_SERVICE);

		// LinearLayout zoomLayout = (LinearLayout) findViewById(R.id.zoom);
		// View zoomView = mapView.getZoomControls();
		//
		// zoomLayout.addView(zoomView, new LinearLayout.LayoutParams(
		// LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
		mapView.displayZoomControls(true);
		mapView.setBuiltInZoomControls(false);
		mapView.getController().setZoom(17);
		// mapview 缓存
		mapView.setDrawingCacheEnabled(true);
		mapView.buildDrawingCache();
		mapView.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_HIGH);
		mapView.preLoad();
		// setMapOnTouch();
		initPopUp();
	}

	private View popView;

	private void initPopUp() {
		Log.i("-------AroundTuanActivity.initPopUp()-------");
		popView = mapView.inflate(this, R.layout.pop_up, null);
		int width = getWindowManager().getDefaultDisplay().getWidth();
		if (width >= 480) {
			mapView.addView(popView, new MapView.LayoutParams(440, 120, null,
					MapView.LayoutParams.BOTTOM_CENTER));
		} else {
			mapView.addView(popView, new MapView.LayoutParams(300, 90, null,
					MapView.LayoutParams.BOTTOM_CENTER));
		}

		// 由于我的气泡的尾巴是在下边居中的,因此要设置成MapView.LayoutParams.BOTTOM_CENTER.
		// 这里没有给GeoPoint,在onFocusChangeListener中设置
		// view.add(popView);
		popView.setVisibility(View.GONE);
		popView.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				String id = (String) popView.getTag();
				// Intent intent = new Intent(AroundTuanActivity.this,
				// DetialActivity.class);
				// intent.putExtra("id", id);
				// startActivity(intent);
			}
		});
	}

	class MyOverlay implements ItemizedOverlay.OnFocusChangeListener {
		private ItemizedOverlay predOver;

		@Override
		public void onFocusChanged(ItemizedOverlay overlay, OverlayItem newFocus) {

			if (popView != null) {
				popView.setVisibility(View.GONE);
			}

			Log.i("overlay : " + overlay);
			Log.i("newFocus : " + newFocus);

			if (newFocus != null) {

				MapView.LayoutParams geoLP = (MapView.LayoutParams) popView
						.getLayoutParams();
				GeoPoint geoPoint = new GeoPoint(newFocus.getPoint()
						.getLatitudeE6(), newFocus.getPoint().getLongitudeE6());

				geoLP.point = geoPoint;// 这行用于popView的定位
				mapView.getController().animateTo(geoLP.point);
				// TextView nameTextView = (TextView) popView
				// .findViewById(R.id.name);
				// TextView location = (TextView) popView
				// .findViewById(R.id.location);
				// ImageView typeImageView = (ImageView) popView
				// .findViewById(R.id.type);
				// TextView timeText = (TextView)
				// popView.findViewById(R.id.time1);

				// String snippet = newFocus.getSnippet();// id#price#tuan_name
				// String[] price_name = snippet.split("#");
				// nameTextView.setText(newFocus.getTitle());
				// location.setText(price_name[1] + "元");
				// tuan_name.setText(price_name[2]);
				// popView.setTag(1);
				// if (!"-1".equals(price_name[3])) {
				// typeImageView.setVisibility(View.VISIBLE);
				// } else {
				// typeImageView.setVisibility(View.GONE);
				// }
				// if (price_name.length > 4) {
				// String userFace = price_name[4];
				// final ImageView user_face = (ImageView) popView
				// .findViewById(R.id.user_face);
				// Bitmap drawable = null;
				// Bitmap drawable = AsyncImageLoader.loadDrawable(userFace,
				// new ImageCallback() {
				//
				// public void imageLoaded(Bitmap imageDrawable,
				// String imageUrl) {
				// if (imageDrawable != null) {
				// user_face.setImageBitmap(imageDrawable);
				// } else {
				// user_face
				// .setImageResource(R.drawable.thum);
				// }
				// }
				// });
				// if (drawable != null) {
				// user_face.setImageBitmap(drawable);
				// } else {
				// user_face.setImageResource(R.drawable.thum);
				// }
				mapView.updateViewLayout(popView, geoLP);
				popView.setVisibility(View.VISIBLE);
			}
		}
	}
}
